import { Container, Sprite, Texture } from "pixi.js";

export var container_map = new Container();
var container_lawn = new Container();
var container_plant = new Container();
var background = Sprite.from("images/map/beij.png");
background.width = innerWidth;
background.height = innerHeight;

interface ILawn {
    lawn_type: "air" | "land" | "water";
    plant: Sprite;
    lawn: Sprite;
}

var map: ILawn[][] = [];

var map_width = 15;
var map_height = 10;
var Globals={
    lawnwidth:100,
    lawnheight:100,
    lawnX: -1,
    lawnY: -1,
};
Globals.lawnX=Globals.lawnX==-1?Math.floor((innerWidth-map_width*Globals.lawnwidth)*0.5):Globals.lawnX;
Globals.lawnY=Globals.lawnY==-1?Math.floor((innerHeight-map_height*Globals.lawnheight)*0.5):Globals.lawnY;
console.log(Globals.lawnX);
for (let x = 0; x < map_width; x++) {
    let data: ILawn[] = [];
    for (let y = 0; y < map_height; y++) {
        let lawn = new Sprite();
        lawn.eventMode = "static";
        lawn.width = Globals.lawnwidth;
        lawn.height = Globals.lawnheight;
        lawn.position.set(x * 100 + Globals.lawnX, y * 100 + Globals.lawnY);
        
        let plant = new Sprite();
        plant.eventMode = "static";
        plant.width = Globals.lawnwidth;
        plant.height = Globals.lawnheight;
        plant.position.set(x * 100 + Globals.lawnX, y * 100 + Globals.lawnY);
        data[y] = { lawn_type: "air", plant, lawn };
        container_lawn.addChild(data[y].lawn);
        container_plant.addChild(data[y].plant);
    }
    map[x] = data;
}
var mapx = [0, 2, 3,5,8,9];
for (let i = 0; i < map.length; i++) {
    for (let j = 0; j < map[i].length; j++) {
        if (mapx.includes(j)) {
            map[i][j].lawn.texture = Texture.from("images/map/水.png");

        } else {
            map[i][j].lawn.texture = Texture.from("images/map/草坪.png");
            map[i][j].lawn_type = "land";
        }
    }
}

container_map.addChild(background, container_lawn, container_plant);

for (let i = 0; i < map.length; i++) {
    for (let j = 0; j < map[i].length; j++) {
        if (mapx.includes(j)) {
            let shui = (map[i][j - 1] && map[i][j - 1].lawn_type === "land"? 1:0)+(map[i][j + 1] && map[i][j + 1].lawn_type === "land"?2:0);
            switch (shui) {
                case 0: map[i][j].lawn.texture = Texture.from("images/map/水.png"); break;
                case 1: map[i][j].lawn.texture = Texture.from("images/map/水上.png"); break;
                case 2: map[i][j].lawn.texture = Texture.from("images/map/水下.png"); break;
                case 3: map[i][j].lawn.texture = Texture.from("images/map/水双.png"); break;
            }
            map[i][j].lawn_type = "water";
        }
    }
}


var container_enemy = new Container();

var Gagas=new Array;
var Gagasmoves=new Array;
function creates(){
var Gaga = Sprite.from("images/gaga/gungunya.gif");
        Gaga.width=Globals.lawnwidth;
        Gaga.height=Globals.lawnheight;
        
        Gaga.x=Globals.lawnX+map_width*Globals.lawnwidth+100
        Gaga.y=Globals.lawnY+rnd(0,map_height-1,"int")*Globals.lawnheight
        
        Gagas.push(Gaga);
        Gagasmoves.push(rnd(0,4,""));
        container_enemy.addChild(Gaga);
        
}
for(let i=0;i<30;i++){
creates();
}
container_map.addChild(container_enemy);



window.setInterval(function(){
    for(let i=0;i<Gagas.length;i++){
        Gagas[i].x-=1+Gagasmoves[i];
    }
},30);


function rnd(rnda:number,rndb:number,rndtype:String){
    return rndtype=="int"?Math.floor(Math.random()*(rndb-rnda+1)+rnda):Math.random()*(rndb-rnda+1)+rnda;
}

function testForAABB(object1: any, object2: any) {
    const bounds1 = object1.getBounds();
    const bounds2 = object2.getBounds();

    return (
        bounds1.x < bounds2.x + bounds2.width
        && bounds1.x + bounds1.width > bounds2.x
        && bounds1.y < bounds2.y + bounds2.height
        && bounds1.y + bounds1.height > bounds2.y
    );
}
